package org.dtrd.modules.patient.entity.bean.report;

import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONUtil;
import com.alibaba.fastjson.JSONObject;
import com.fasterxml.jackson.annotation.JsonFormat;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import org.dtrd.common.util.DateUtils;
import org.dtrd.config.system.Constant;
import org.dtrd.modules.evaluation.entity.bean.report.TotalEvaluationReports;
import org.dtrd.modules.patient.entity.po.DtrdEntRdReport;

import java.util.Date;
import java.util.Map;

/**
 * <p>
 * 患者报告
 * </p>
 *
 * @author QR
 * @since 2022-01-22
 */
@Getter
@Setter
@ApiModel(value = "患者首诊报告")
@NoArgsConstructor
public class ReportFirst {

    private Integer dataId;

    @ApiModelProperty("患者id")
    private Integer patientId;

    @ApiModelProperty("报告名字")
    private String reportName;

    @ApiModelProperty("发布状态, 0-未发布, 1-已发布")
    private Integer publishStatus = 0;
    @ApiModelProperty("发布状态文本")
    private String publishStatusText;

    @ApiModelProperty("报告创建时间")
    @JsonFormat(pattern = Constant.TIME_PATTERN_SDF, timezone = Constant.TIME_ZONE_1)
    private Date reportCreateTime;

    @ApiModelProperty("报告编号")
    private String reportId;

    @ApiModelProperty("患者姓名")
    private String patientsName;

    @ApiModelProperty("患者性别")
    private String patientsSex;

    @ApiModelProperty("患者年龄")
    private Integer patientAge;

    @ApiModelProperty("患者档案")
    private PatientArchive patientArchive;

    @ApiModelProperty("量表评估")
    private TotalEvaluationReports patientEvaluation;

    @ApiModelProperty("服务方案")
    private PatientServiceProgramme patientServiceProgramme;

    @ApiModelProperty("报告单")
    private String patientArchiveReport;

    @ApiModelProperty("专家评估")
    private ReportFirstUpdateRequest firstEvaluate;

    @ApiModelProperty("血糖chart")
    private Map<String, String[]> sugarChart;

    @ApiModelProperty("ogtt chart")
    private Map<String, String[]> ogttChart;

    @ApiModelProperty("制表人")
    private String creator;

    public ReportFirst(Integer patientId) {
        this.patientId = patientId;
    }

    @ApiModel("首诊报告数据")
    @Getter
    @Setter
    public static class FirstReportContent {
        @ApiModelProperty("基本信息")
        private PatientBasicInfo basicInfo;
    }

    public ReportFirst parseFromPO(DtrdEntRdReport po) {
        reportCreateTime = po.getCreateTime();
        if (po.getPublishStatus() != null) {
            publishStatus = po.getPublishStatus();
            publishStatusText = Constant.ReportPublishType.valueOf(po.getPublishStatus()).getName();
        } else {
            publishStatus = Constant.ReportPublishType.NO_PUBLISH.getType();
            publishStatusText = Constant.ReportPublishType.NO_PUBLISH.getName();
        }
        dataId = po.getDataId();
        patientId = po.getPatientId();
        reportName = po.getReportName();
        patientArchive = JSONObject.parseObject(po.getPatientArchive(), PatientArchive.class);
        patientEvaluation = JSONObject.parseObject(po.getPatientEvaluation(), TotalEvaluationReports.class);
        patientServiceProgramme = JSONObject.parseObject(po.getPatientServiceProgramme(), PatientServiceProgramme.class);
        patientArchiveReport = po.getPatientArchiveReport();
        firstEvaluate = JSONObject.parseObject(po.getFirstEvaluate(), ReportFirstUpdateRequest.class);
        reportId = DateUtils.date2Str(po.getCreateTime(),DateUtils.short_date_sdf_ym.get()) + po.getDataId().toString();
        this.patientsName = JSONUtil.toBean(po.getPatientArchive(), FirstReportContent.class).getBasicInfo().getPatientsName();
        this.patientsSex = JSONUtil.toBean(po.getPatientArchive(), FirstReportContent.class).getBasicInfo().getPatientsSex();
        this.patientAge = JSONUtil.toBean(po.getPatientArchive(), FirstReportContent.class).getBasicInfo().getPatientAge();
        return this;
    }
}
